;tape save/load routines


;updates saveload_buffer from tape
;ret a=0 if no error, a=1 reding header error, a=2 reading data error
loadgame_from_tape:

	call	0x00E1								;read header
	jr		c,loadgame_from_tape_error_header
	
	ld		b,115
	ld		hl,saveload_buffer
tape_data_read_loop:
	push	bc
	push	hl
	
	call	0x00E4
	jr		c,loadgame_from_tape_error_data
	
	pop		hl
	ld		[hl],a
	inc		hl
	pop		bc
	djnz	tape_data_read_loop

	call	0x00E7								;stops reading from the tape
	xor		a
	ret

loadgame_from_tape_error_header:
	ld		a,1
	ret

loadgame_from_tape_error_data
	ld		a,2
	ret


;saveload_buffer must be already updated
;ret a=0 no error, a=1 error writing header, a=2 error writing data
savegame_on_tape:
	
	
	;write header
	ld		a,0					;long header
	call	0x00EA
	jr		c,savegame_on_tape_error_header
	
	ld		b,115
	ld		hl,saveload_buffer
savegame_on_tape_loop:
	push	bc
	push	hl
	
	ld		a,[hl]
	call	0x00ED
	jr		c,savegame_on_tape_error_data
	
	pop		hl
	inc		hl
	pop		bc
	djnz	savegame_on_tape_loop

	call	0x00F0			;stops writing on the tape
	
	xor		a
	ret
	
savegame_on_tape_error_header:
	ld		a,1
	ret
	
savegame_on_tape_error_data	
	ld		a,2
	ret
	
	
